/**
 * Created by Hank on 2015/7/22.
 */
var sort;
var editRow = 0;
$(function(){
    initTable('00');
});

//初始化表格
function initTable(type){
    $('#tt').treegrid({
        width : backendCommon.getWidth(0.80),
        height : backendCommon.getHeight(0.50),
        url:mainDomain + '/operator/queryInfoByObjType.action',
        queryParams: {locType: '4', type: type},
        rownumbers : true,
        animate : true,
        loadMsg : '数据加载中...',
        fitColumns : true,
        singleSelect : true,
        idField:'id',
        columns:[[
            {title:'顺序',field:'infoSort',width:120,align:'center',
                formatter : function(value, rec, index) {
                    var str = value;
                    return str;
                },
                editor:'text'
            },
            {title:'封面图片',field:'infoUrl',width:150,align:'center'},
            {title:'资讯标题',field:'infoTitle',width:160,align:'center'},
            {title : '操  作',field : 'isPublish',width:320,align : 'center',
                formatter : function(value, rec, index) {
                    var str = '';
                    if(rec.isPublish == 1) {
                        str = '<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoState(this,'+rec.id+');" data-value="0">下架</a>';
                    } else {
                        str = '<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoState(this,'+rec.id+');" data-value="1">上架</a>';
                    }
                    if(rec.isTop == 1){
                        str += '&nbsp;&nbsp;&nbsp;<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoTop(this,'+rec.id+');" data-value="0">不顶置</a>'
                    } else {
                        str += '&nbsp;&nbsp;&nbsp;<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoTop(this,'+rec.id+');" data-value="1">顶置</a>'
                    }
                    return str;
                }
            }
        ]],
        onBeforeEdit : function(row) {
            row.editing = true;
            sort = row.infoSort;
            $('#tt').treegrid('refresh', row.id);
            editRow++;
        },
        onAfterEdit : function(row,changes) {
            row.editing = false;
            $('#tt').treegrid('refresh', row.id);
            editRow--;
        },
        onCancelEdit : function(row) {
            row.editing = false;
            $('#tt').treegrid('refresh', row.id);
            editRow--;
        },
        onDblClickCell : function(field,row) {
            if(editRow > 0) {
                $.messager.alert('提示', '其他数据正在编辑中，请保存后再执行其他操作', 'error');
                return;
            }
            $('#tt').datagrid('beginEdit', row.id);
            $("input.datagrid-editable-input").focus();
            $("input.datagrid-editable-input").keyup(function(e) {
                if(e.keyCode == 13) {
                    saveInfoSort(field,row);
                }
            });
        }
    }).datagrid('acceptChanges');

    $('#tt1').treegrid({
        width : backendCommon.getWidth(0.80),
        height : backendCommon.getHeight(0.50),
        url:mainDomain + '/operator/queryInfoByObjType.action',
        queryParams: {locType: '5', type: type},
        rownumbers : true,
        animate : true,
        loadMsg : '数据加载中...',
        fitColumns : true,
        singleSelect : true,
        idField:'id',
        columns:[[
            {title:'顺序',field:'infoSort',width:120,align:'center',
                formatter : function(value, rec, index) {
                    var str = value;
                    return str;
                },
                editor:'text'
            },
            {title:'封面图片',field:'infoUrl',width:150,align:'center'},
            {title:'资讯标题',field:'infoTitle',width:160,align:'center'},
            {title : '操  作',field : 'isPublish',width:320,align : 'center',
                formatter : function(value, rec, index) {
                    var str = '';
                    if(rec.isPublish == 1) {
                        str = '<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoState(this,'+rec.id+');" data-value="0">下架</a>';
                    } else {
                        str = '<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoState(this,'+rec.id+');" data-value="1">上架</a>';
                    }
                    if(rec.isTop == 1){
                        str += '&nbsp;&nbsp;&nbsp;<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoTop(this,'+rec.id+');" data-value="0">不顶置</a>'
                    } else {
                        str += '&nbsp;&nbsp;&nbsp;<a class="btn_blue06" href="javascript:void(0);" onclick="saveInfoTop(this,'+rec.id+');" data-value="1">顶置</a>'
                    }
                    return str;
                }
            }
        ]],
        onBeforeEdit : function(row) {
            row.editing = true;
            sort = row.infoSort;
            $('#tt1').treegrid('refresh', row.id);
            editRow++;
        },
        onAfterEdit : function(row,changes) {
            row.editing = false;
            $('#tt1').treegrid('refresh', row.id);
            editRow--;
        },
        onCancelEdit : function(row) {
            row.editing = false;
            $('#tt1').treegrid('refresh', row.id);
            editRow--;
        },
        onDblClickCell : function(field,row) {
            if(editRow > 0) {
                $.messager.alert('提示', '其他数据正在编辑中，请保存后再执行其他操作', 'error');
                return;
            }
            $('#tt1').datagrid('beginEdit', row.id);
            $("input.datagrid-editable-input").focus();
            $("input.datagrid-editable-input").keyup(function(e) {
                if(e.keyCode == 13) {
                    saveInfoSort(field,row);
                }
            });
        }
    }).datagrid('acceptChanges');
}

//资讯状态变更
function saveInfoState($this,id){
    //资讯状态
    var publishState = $($this).attr("data-value");
    var param = {"id":id,'publishState':publishState};
    var o = $($this);
    $.post(mainDomain + '/operator/updateInfoState.action', param, function(result){
        if (result.success) {
            if(publishState=="1"){
                o.text("下架");
                o.attr("data-value","0");
            }
            if(publishState=="0"){
                o.text("上架");
                o.attr("data-value","1");
            }
            $.messager.alert('提示', '操作成功', 'info');
        } else {
            $.messager.alert('提示', '操作失败', 'error');
        }
    },'json');
}

//保存资讯顶置
function saveInfoTop($this,id) {
    //资讯状态
    var isTop = $($this).attr("data-value");
    var param = {"id":id,'isTop':isTop};
    var o = $($this);
    $.post(mainDomain + '/operator/updateInfoTop.action', param, function(result){
        if (result.success) {
            if(isTop=="1"){
                o.text("不顶置");
                o.attr("data-value","0");
            }
            if(isTop=="0"){
                o.text("顶置");
                o.attr("data-value","1");
            }
            $.messager.alert('提示', '操作成功', 'info');
        } else {
            $.messager.alert('提示', '操作失败', 'error');
        }
    },'json');
}

//保存排序结果
function saveInfoSort(field,row) {
    var t = $("#tt");
    t.treegrid('endEdit', row.id);
    //修改之前
    var sortVal = sort;
    //修改之后
    var sortAfter = row.infoSort;//  t.treegrid('getSelected').sort;
    //修改之前和修改之后一样   不修改
    if(sortAfter!='' && sortVal != sortAfter){
        //如果值不相同，则保存
        var param = {
            id : row.id,
            index : sortAfter
        };
        //新增编辑栏目
        $.post(mainDomain + '/operator/updateInfoSort.action', param ,function(result){
            if (!result.success) {
                //如果失败，则提示错误信息
                $.messager.alert('提示', result.message, 'error');
            }
        },'json');
    }
}

//页面上的首页版本选择
function getLocType(){
    var typeCode = $("#infoType").val();
    initTable(typeCode);
}